$().ready(function() {
	initDictDatas();
	validateRule();
	initFileUpload();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
//构建字典数据
function initDictDatas(){
	
	var thatDictFields = window.parent.dictFields;
	var thatDictkeys = window.parent.dictKeys;
	var thatDictDatas = window.parent.dicDatas;
	
	//省
	$('#provinceNum').bootstrapSelect({
	    url:ctx+'plat/hyBaseArea/list/100000',
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属省",
	    onChange:function(value){
	    	//TODO 当选择为空时,不触发
	    	if(value == null || value == ''){
	    		return false;
	    	}
	    	//级联市
	    	$('#cityNum').bootstrapSelect({
	    		url:ctx+'plat/hyBaseArea/list/'+value,
	    		valueField: 'areaNum',
	    		textField: 'areaName',
	    		placeholder: "请选择所属市"
	    	});
	    	//bootstrap-select使用规则是先执行操作然后refresh
	    	$('#cityNum').selectpicker('refresh');
	    }
	});
	$('#cityNum').bootstrapSelect({
	    valueField: 'areaNum',
	    textField: 'areaName',
	    placeholder: "请选择所属市"
	});
	$('#platType').bootstrapSelect({
		data:thatDictDatas['platType'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择平台类型"
	});
	$('#platFlag').bootstrapSelect({
		data:thatDictDatas['plat_info_status'],
		valueField: 'value',
		textField: 'name',
		placeholder: "请选择状态"
	});
	$('#platType').bootstrapSelect('setValue',1);
}
function initFileUpload(){
	$("#platLogo").fileinput({
		'language' : 'zh',
		'showUpload':false,
		'previewFileType':'any',
		'maxFileSize':'200',//限制200kb
		'dropZoneTitle':'您可以拖拽文件到这里&hellip;<br>大小不要超过200kb'
	});
	
}

function save() {
	var formData = new FormData(document.getElementById("signupForm"));
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"plat/platInfo/save",
//		data : $('#signupForm').serialize(),// 你的formid
		data : formData,// 你的formid
		contentType:false,
		processData:false,
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			platName : {
				required : true
			},			
			platType : {
				required : true
			},			
			provinceNum : {
				required : true
			},	
			cityNum : {
				required : true
			},
			platFlag : {
				required : true
			}
		},
		messages : {

			platName : {
				required : icon + "请输入平台名称"
			},			
			platType : {
				required : icon + "请选择平台类型"
			},			
			provinceNum : {
				required : icon + "请选择所属省"
			},	
			cityNum : {
				required : icon + "请选择所属市"
			},
			platFlag : {
				required : icon + "请选择平台状态"
			}
		},
		errorPlacement: function(error, element) {
			if (element[0].tagName == 'SELECT'){//nodeName
				error.appendTo(element.parent().parent());
			}else {					
				error.appendTo(element.parent());
//				error.insertAfter(element);
			}
		},
		success:function(lable){
			lable.parents(".tip").remove();
			console.log(lable);
		}
	})
}